package com.hongkuncheng.cms.freemaker.admin;

import java.util.List;

import org.springframework.stereotype.Component;
import com.hongkuncheng.cms.helper.AdminHelper;
import com.hongkuncheng.cms.model.Admin;
import com.hongkuncheng.cms.model.AdminRole;
import freemarker.ext.beans.BeanModel;
import freemarker.template.TemplateMethodModelEx;
import freemarker.template.TemplateModelException;

@Component
public class CheckPermissionMethod implements TemplateMethodModelEx {

    @Override
    public Object exec(List args) throws TemplateModelException {
        Class clazz = null;
        try {
            clazz = Class.forName(args.get(0).toString().substring(0, args.get(0).toString().indexOf("@")));
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        boolean result = false;
        BeanModel mo = (BeanModel) args.get(0);
        if (clazz.equals(Admin.class)) {
            result = AdminHelper.checkAdminPermission((Admin) mo.getAdaptedObject(Admin.class), args.get(1).toString());
        } else if (clazz.equals(AdminRole.class)) {
            result = AdminHelper.checkAdminRolePermission((AdminRole) mo.getAdaptedObject(AdminRole.class),
                    args.get(1).toString());
        }
        return result;
    }

}
